<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) 2015-2016 http://www.clcw.com.cn All rights reserved.    |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: Xmh <xiangminghu@clcw.com.cn>                                  |
// +------------------------------------------------------------------------+
//

namespace AuctionAppApi\Controller;

use Think\Controller;
use AuctionCore\System\AuctionException;
use AuctionCore\AuctionOrder\AuctionSetting;
use AuctionCore\Common\Util;

class UpgradeController extends Controller {

    public function __construct(){
        parent::__construct();
        //更改异常处理程序
        set_exception_handler('AuctionCore\System\AuctionException::appException');
    }

    /*
     * 检查APP更新接口
     * @param sort int 应用类型
     * @param driver int 应用设备
     * @param version int 版本号
     * return json
     * */
    public function index() {
        $rep = array();
        $sort_id = I('post.sort', 2);
        $driver = I('post.driver', 0);
        $version = I('post.version', '');

        if ($sort_id < 1 || $driver < 1 || $version == '') {
            AuctionException::make_throw('800050');
        } else {
            $condition = array('sort_id' => $sort_id, 'driver' => $driver, 'status' => 1);
            $lastest = M('client_app')->where($condition)->order('`id` DESC')->find();
            if ($lastest) {
                if (version_compare($version, $lastest['version'], '>=')) {
                    AuctionException::make_throw('800051');
                } else {
                    $rep['msg'] = 'SUCCESS';
                    $rep['name'] = $lastest['name'];
                    $rep['version'] = $lastest['version'];
                    $rep['url'] = $lastest['link'];
                    $rep['summary'] = $lastest['note'];
                    $rep['force'] = $lastest['force']?true:false;
                }
            } else {
                AuctionException::make_throw('800052');
            }
        }
        echo Util::auto_jsonp_or_json($_REQUEST,array('code'=>200,'msg'=>'success','data'=>$rep));
        exit;
    }
    
    /*
     * 检查APP更新接口
     * @param sort int 应用类型
     * @param driver int 应用设备
     * @param version int 版本号
     * return json
     * */

    public function index_v140() {
        $rep = array();
        $sort_id = I('post.sort', 2);
        $driver = I('post.driver', 0);
        $version = I('post.version', '');

        $platform = I('request.platform');

        //获取审核版本
        $setting = new AuctionSetting();
        $set_name = $platform=='ios'?'IOS_APP_AUDITING_VER': 'ANDROID_APP_AUDITING_VER';
        $set_info = $setting->detail_by_name($set_name);
        $rep['auditing'] = version_compare($set_info['value'], $version, '=') ? true :false;
        //
        if ($sort_id < 1 || $driver < 1 || $version == '') {
            $code = '800050';
        } else {
            $condition = array('sort_id' => $sort_id, 'driver' => $driver, 'status' => 1);
            $lastest = M('client_app')->where($condition)->order('`id` DESC')->find();
            if ($lastest) {
                if (version_compare($version, $lastest['version'], '>=')) {
                    $code = '800051';
                } else {
                    $code = '200';
                    $rep['name'] = $lastest['name'];
                    $rep['version'] = $lastest['version'];
                    $rep['url'] = $lastest['link'];
                    $rep['summary'] = $lastest['note'];
                    $rep['force'] = $lastest['force']?true:false;
                }
            } else {
                $code = '800052';
            }
        }
        $msg = $code == '200' ?  'SUCCESS' : AuctionException::$codes[$code];
        echo Util::auto_jsonp_or_json($_REQUEST,array('code'=>$code,'msg'=>$msg,'data'=>$rep));
        exit;
    }

}
